package ink.xiaobaibai.serviceOxy.emqtt.impl;

import com.alibaba.fastjson.JSON;
import ink.xiaobaibai.emqtt.EMqttManager;
import ink.xiaobaibai.emqtt.EMqttService;
import ink.xiaobaibai.emqtt.MyMqttCallback;
import ink.xiaobaibai.serviceOxy.emqtt.IBaseEMqttService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-07-23
 **/

@Service
@Slf4j
public class BaseEMqttServiceImpl implements IBaseEMqttService {

    private final Random random = new Random();

    @Override
    public boolean check(String topic) {
        String clientId = "" + System.currentTimeMillis() + this.random.nextInt(10000);
        List<String> list = new ArrayList<>();
        EMqttService client = EMqttManager.getClient(topic, topic, clientId, new MyMqttCallback() {
            @Override
            protected void connectionLost2(Throwable throwable) {
                log.error("连接失败");
            }

            @Override
            protected void messageArrived2(String s, MqttMessage mqttMessage) {
                String ss = new String(mqttMessage.getPayload());
                list.add(ss);
            }

            @Override
            protected void deliveryComplete2(IMqttDeliveryToken iMqttDeliveryToken) {
            }
        });
        client.start();
        client.pushMessage("hello");
        // 给2.5秒响应
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        client.close();

        for (String str : list) {
            if (!"hello".equals(str)) {
                return true;
            }
        }

        return false;
    }

    @Override
    public void pushMsg(String topic, Object msg) {
        String clientId = "" + System.currentTimeMillis() + this.random.nextInt(10000);
        EMqttService client = EMqttManager.getClient(null, topic, clientId, new MyMqttCallback() {
            @Override
            protected void connectionLost2(Throwable throwable) {
                log.error("连接失败");
            }

            @Override
            protected void messageArrived2(String s, MqttMessage mqttMessage) {
                log.error("不可能接受消息");
            }

            @Override
            protected void deliveryComplete2(IMqttDeliveryToken iMqttDeliveryToken) {

            }
        });
        client.start();
        client.pushMessage(JSON.toJSONString(msg));
        client.close();
    }

    public static void main(String[] args) {
        EMqttService clientTest = EMqttManager.getClientTest("abc5", "abc5", "asd", new MyMqttCallback() {
            @Override
            protected void connectionLost2(Throwable throwable) {

            }

            @Override
            protected void messageArrived2(String s, MqttMessage mqttMessage) {
                String str = new String(mqttMessage.getPayload());
                System.out.println(str);
                this.geteMqttService().pushMessage("qweqwe");
            }

            @Override
            protected void deliveryComplete2(IMqttDeliveryToken iMqttDeliveryToken) {

            }
        });
        clientTest.start();
    }

}
